package com.bw.cinema.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.bw.cinema.adapter.MutilVideoAdapter
import com.bw.cinema.adapter.SubMutilTypeAdapter
import com.bw.cinema.databinding.FragmentCinemaBinding
import com.bw.cinema.entity.MutilTypeEntity
import com.bw.cinema.entity.MutilVideoEntity
import com.bw.cinema.intent.MutilTypeIntent
import com.bw.cinema.intent.MutilVideoIntent
import com.bw.cinema.state.MutilTypeState
import com.bw.cinema.state.MutilVideoState
import com.bw.cinema.viewmodel.MutilTypeViewModel
import com.bw.cinema.viewmodel.MutilVideoViewModel
import com.bw.lib_mvi.view.BaseMVIFragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import kotlinx.coroutines.launch


class CinemaFragment : BaseMVIFragment() {
    lateinit var binding: FragmentCinemaBinding
    lateinit var mutilTypeViewModel: MutilTypeViewModel
    lateinit var subMutilTypeAdapter: SubMutilTypeAdapter
    val subList: MutableList<MutilTypeEntity> = mutableListOf()

    lateinit var mutilVideoViewModel: MutilVideoViewModel
    lateinit var mutilVideoAdapter: MutilVideoAdapter
    val mutilVideoList: MutableList<MutilVideoEntity> = mutableListOf()

    var typeId: Int = 1

    var page: Int = 1
    var size: Int = 10
    var videoId: Int = 20

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentCinemaBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun initEvent() {
        super.initEvent()
        binding.tabCinema.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                if (tab?.tag != null) {
                    val entity: MutilTypeEntity = tab?.tag as MutilTypeEntity
                    lifecycleScope.launch {
                        //TODO
                        mutilTypeViewModel.intents.send(MutilTypeIntent.getSubMutilType(entity.id))
                    }
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })
    }

    override fun generateViewModel() {
        mutilTypeViewModel =
            ViewModelProvider(requireActivity()).get(MutilTypeViewModel::class.java)
        mutilVideoViewModel =
            ViewModelProvider(requireActivity()).get(MutilVideoViewModel::class.java)
        loadMutilType()
        initSubMutilType()
        loadMutilVideo()
    }

    private fun loadMutilVideo() {
        mutilVideoAdapter = MutilVideoAdapter(requireContext(), mutilVideoList)
        binding.rvMutilVideoCinema.adapter = mutilVideoAdapter
        binding.rvMutilVideoCinema.layoutManager = GridLayoutManager(requireContext(), 2)
    }

    private fun initSubMutilType() {
        subMutilTypeAdapter = SubMutilTypeAdapter(requireContext(), subList)
        binding.rvSubMutiltypeCinema.adapter = subMutilTypeAdapter
        binding.rvSubMutiltypeCinema.layoutManager = GridLayoutManager(requireContext(), 4)

        subMutilTypeAdapter.setSubMutilClick(object : SubMutilTypeAdapter.SubMutilVideoClickListen {
            override fun click(view: View, entity: MutilTypeEntity) {
                lifecycleScope.launch {
                    videoId = entity.id
                    mutilVideoViewModel.intents.send(
                        MutilVideoIntent.getMutilVideoIntent(
                            page,
                            size,
                            videoId
                        )
                    )
                }
            }
        })
    }

    private fun loadMutilType() {
        lifecycleScope.launch {
            mutilTypeViewModel.intents.send(MutilTypeIntent.getMutilType)
        }
    }

    override fun handlerState() {
        lifecycleScope.launch {
            mutilTypeViewModel.state.collect {
                when (it) {

                    is MutilTypeState.getMutilTypeState -> {
                        if (it.data.isNotEmpty()) {
                            for (type in it.data) {
                                binding.tabCinema.addTab(
                                    binding.tabCinema.newTab().setText(type.typename).setTag(type)
                                )
                            }
                        }
                    }

                    is MutilTypeState.getSubMutilTypeState -> {
                        subList.clear()
                        subList.addAll(it.data)
                        subMutilTypeAdapter.notifyDataSetChanged()
                        videoId = it.data.get(0).id
                        mutilVideoViewModel.intents.send(
                            MutilVideoIntent.getMutilVideoIntent(
                                page,
                                size,
                                videoId
                            )
                        )
                    }

                    else -> {}
                }
            }
        }

        lifecycleScope.launch {
            mutilVideoViewModel.state.collect {
                when (it) {

                    is MutilVideoState.getMutilVideoState -> {
                        mutilVideoList.clear()
                        mutilVideoList.addAll(it.data)
                        mutilVideoAdapter.notifyDataSetChanged()
                    }

                    else -> {}
                }
            }
        }
    }

}